package server

import (
	v1 "gin-vben-admin/api/v1"
	"gin-vben-admin/internal/handler"
	"gin-vben-admin/internal/middleware"
	"gin-vben-admin/pkg/cfgstruct"
	"gin-vben-admin/pkg/jwt"
	"gin-vben-admin/pkg/server/http"
	"github.com/gin-gonic/gin"
	"github.com/opdss/version"
	"go.uber.org/zap"
)

var registerRouters = make([]func(r gin.IRouter), 0)

func NewHTTPServer(
	log *zap.Logger,
	conf *http.Config,
	jwt *jwt.JWT,
	commonHandler *handler.CommonHandler,
	loginHandler *handler.LoginHandler,
	assetsHandler *handler.AssetsHandler,
	sysUserHandler *handler.SysUserHandler,
	sysMenuHandler *handler.SysMenuHandler,
	sysRoleHandler *handler.SysRoleHandler,
	sysDeptHandler *handler.SysDeptHandler,
) *http.Server {
	switch cfgstruct.DefaultsType() {
	case cfgstruct.DefaultsRelease:
		gin.SetMode(gin.ReleaseMode)
	case cfgstruct.DefaultsTest:
		gin.SetMode(gin.TestMode)
	default:
		gin.SetMode(gin.DebugMode)
	}
	s := http.NewServer(
		gin.Default(),
		log,
		conf,
	)
	//注册静态web路由
	assetsHandler.Register(s.Engine)

	//外部handler
	for i := range registerRouters {
		registerRouters[i](s)
	}
	s.Use(middleware.CORSMiddleware())
	apiGroup := s.Group("/api",
		middleware.RequestLogMiddleware(log.Named("middleware")),
	)

	// No route group has permission
	apiGroup.POST("/login", loginHandler.Login)
	apiGroup.POST("/refresh_token", loginHandler.RefreshToken)
	apiGroup.GET("/captcha", commonHandler.GetCaptcha)
	//版本信息
	apiGroup.GET("/version", func(ctx *gin.Context) {
		v1.Success(ctx, version.Build)
	})

	//需要登陆
	authRouter := apiGroup.Group("", middleware.Auth(jwt, log))
	{
		authRouter.GET("/profile", loginHandler.Profile)
		authRouter.POST("/logout", loginHandler.Logout)

		sysRouter := authRouter.Group("/system")
		{

			//sysRouter.GET("/user", sysUserHandler.PageList)
			//sysRouter.GET("/user/:id", sysUserHandler.Detail)
			sysRouter.GET("/user", sysUserHandler.GetPageList)
			sysRouter.GET("/user/:uid", sysUserHandler.Detail)
			sysRouter.POST("/user", sysUserHandler.Create)
			sysRouter.PUT("/user", sysUserHandler.Update)
			sysRouter.DELETE("/user/:uid", sysUserHandler.Delete)
			sysRouter.PUT("/user/:uid/enable", sysUserHandler.Enable)
			sysRouter.PUT("/user/:uid/disable", sysUserHandler.Disable)
			sysRouter.PUT("/user/reset_password", sysUserHandler.ResetPassword)

			sysRouter.GET("/menu/tree", sysMenuHandler.GetTreeList)
			sysRouter.GET("/menu/:id", sysMenuHandler.Detail)
			sysRouter.POST("/menu", sysMenuHandler.Create)
			sysRouter.PUT("/menu", sysMenuHandler.Update)
			sysRouter.POST("/menu/action", sysMenuHandler.CreateAction)
			sysRouter.PUT("/menu/action", sysMenuHandler.UpdateAction)

			sysRouter.GET("/dept/tree", sysDeptHandler.GetTreeList)
			sysRouter.POST("/dept", sysDeptHandler.Create)
			sysRouter.PUT("/dept", sysDeptHandler.Update)
			sysRouter.DELETE("/dept/:id", sysDeptHandler.Delete)

			sysRouter.GET("/role", sysRoleHandler.GetPageList)
			sysRouter.GET("/role/options", sysRoleHandler.GetOptions)
			sysRouter.POST("/role", sysRoleHandler.Create)
			sysRouter.PUT("/role", sysRoleHandler.Update)
			sysRouter.DELETE("/role/:id", sysRoleHandler.Delete)
		}
	}
	//v1 := s.Group("/v1")
	//{
	//	// No route group has permission
	//	noAuthRouter := v1.Group("/")
	//	{
	//		noAuthRouter.POST("/register", userHandler.Register)
	//		noAuthRouter.POST("/login", userHandler.Login)
	//	}
	//	// Non-strict permission routing group
	//	noStrictAuthRouter := v1.Group("/").Use(middleware.NoStrictAuth(jwt, logger))
	//	{
	//		noStrictAuthRouter.GET("/user", userHandler.GetProfile)
	//	}
	//
	//	// Strict permission routing group
	//	strictAuthRouter := v1.Group("/").Use(middleware.StrictAuth(jwt, logger))
	//	{
	//		strictAuthRouter.PUT("/user", userHandler.UpdateProfile)
	//	}
	//}
	return s
}
